package MODELS;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import ModelEntityFW.HosoBAEntity;
import ModelEntityFW.UserEntitys;

public class HoSoBAModels extends BaseModels<HosoBAEntity> {

	public HoSoBAModels(Connection objConnection, UserEntitys objSecurity) {
		super(objConnection, objSecurity);
	}

	@Override
	public ResultSet GetElements() throws Exception {
        try {
            // 1. Kiểm tra trạng thái kết nối
            if (!m_objConnection.isClosed()) {
                m_objCmst = m_objConnection.prepareCall("{call SP_HIENTHIHOSOBA()}");
                // 2. Thực thi truy vấn
                m_objRs = m_objCmst.executeQuery();
            }
        } catch (Exception ex) {
            throw ex;
        }
		return m_objRs;
	}

	@Override
	public ResultSet GetElementById(HosoBAEntity obj) throws Exception {
		return null;
	}

	@Override
	public void CheckElement(HosoBAEntity obj) throws Exception {
		
	}

	@Override
	public boolean InsertElement(HosoBAEntity obj) throws Exception {
		try{
			if(!m_objConnection.isClosed()){
				m_objCmst = m_objConnection.prepareCall("{call SP_THEMHOSOBA(?,?,?,?,?)}");
				
				m_objCmst.setString("NgayTaoHS", obj.getNgayTaoHS());
				m_objCmst.setString("NguoiTaoHS", obj.getNguoiTaoHS());
				m_objCmst.setInt("BHYT", obj.getBHYT());
				m_objCmst.setInt("BHXH", obj.getBHXH());
				m_objCmst.setInt("BN_ID", obj.getBN_ID());
				
				m_objCmst.execute();
			}
		}catch(SQLException ex){
			throw ex;
		}
		return true;
	}

	@Override
	public boolean UpdateElement(HosoBAEntity obj) throws Exception {
		try{
			if(!m_objConnection.isClosed()){
				m_objCmst = m_objConnection.prepareCall("{call SP_CAPNHATHOSOBA(?,?,?,?,?,?)}");
				
				m_objCmst.setInt("HoSoID",obj.getHoSoID());
				m_objCmst.setString("NgayTaoHS", obj.getNgayTaoHS());
				m_objCmst.setString("NguoiTaoHS", obj.getNguoiTaoHS());
				m_objCmst.setInt("BHYT", obj.getBHYT());
				m_objCmst.setInt("BHXH", obj.getBHXH());
				m_objCmst.setInt("BN_ID", obj.getBN_ID());
				
				m_objCmst.execute();
			}
		}catch (SQLException ex){
			throw ex;
		}
		return true;
	}

	@Override
	public boolean DeleteElement(HosoBAEntity obj) throws Exception {
		try{
			if(!m_objConnection.isClosed()){
				m_objCmst=m_objConnection.prepareCall("{call SP_XoaHOSOBA(?)}");
				
				m_objCmst.setInt("HoSoID", obj.getHoSoID());
				
				m_objCmst.execute();
			}
		}catch(SQLException ex){
			throw ex;
		}
		return true;
	}

	@Override
	public boolean FindElement(HosoBAEntity obj) throws Exception {
		return false;
	}
	
	
}
